const Router = require('koa-router');
const router = new Router();
const {db, QueryTypes, } = require('../models/db')
const pageParams = require("../tools/cols");
const tools = require("../tools/tool");

// 库存统计
router.post('/stock/kctj', async ctx => {
    let whichPage = ctx.request.body.whichPage;
    let formData = ctx.request.body.formData;
    let currentPage = ctx.request.body.currentPage ?ctx.request.body.currentPage:1;
    let pageSize = ctx.request.body.pageSize?ctx.request.body.pageSize:200;
    let excel = ctx.request.body.excel?ctx.request.body.excel:0;
    let sort = ctx.request.body.sort
    let offset = (currentPage - 1) * pageSize;
    if(!whichPage) return ctx.err('没有模型名')
    let params = pageParams(ctx, tools) // cols文件中的列配置
    let sql = params.sql
    sql = sql.replaceAll('$tid', ctx.user.tenantId)
    sql = sql.replaceAll('$uid', ctx.user.id)
    if(formData.beginDate) {
        sql = sql.replaceAll('$beginDate', formData.beginDate)
    }
    if(formData.endDate) {
        sql = sql.replaceAll('$endDate', formData.endDate)
    }
    if(formData.invId) sql += ' and invId='+formData.invId
    if(formData.locationId) sql += ' and locationId='+formData.locationId
    if(!formData.lastC) {
        sql += ` and lastC <> 0 `
    }
    if(!formData.bizC) {
        sql += ` and (inC <> 0 or outC <> 0) `
    }
    sql += params.order
    let list = await db.query(sql, {type: QueryTypes.SELECT})
    list.map(row => {
        row.initC = row.initC ? row.initC:''
        row.initM = row.initM ? row.initM.toFixed(2):''
        row.inC = row.inC ? row.inC:''
        row.inM = row.inM ? row.inM.toFixed(2):''
        row.outC = row.outC ? row.outC:''
        row.outM = row.outM ? row.outM.toFixed(2):''
        row.lastC = row.lastC ? row.lastC:''
        row.lastM = row.lastM ? row.lastM.toFixed(2):''
    })

    if(excel) {
        return await tools.exportOut(params, list, ctx)
    }
    return ctx.suc('', {
        total: 0,
        list
    })
})
// 库存明细
router.post('/stock/kcmx', async ctx => {
    let whichPage = ctx.request.body.whichPage;
    let formData = ctx.request.body.formData;
    let currentPage = ctx.request.body.currentPage ?ctx.request.body.currentPage:1;
    let pageSize = ctx.request.body.pageSize?ctx.request.body.pageSize:200;
    let excel = ctx.request.body.excel?ctx.request.body.excel:0;
    let sort = ctx.request.body.sort
    let offset = (currentPage - 1) * pageSize;
    if(!whichPage) return ctx.err('没有模型名')
    let params = pageParams(ctx, tools) // cols文件中的列配置
    let sql = params.sql

    sql = sql.replaceAll('$tid', ctx.user.tenantId)
    sql = sql.replaceAll('$uid', ctx.user.id)
    if(formData.beginDate) {
        sql = sql.replaceAll('$beginDate', formData.beginDate)
    }
    if(formData.endDate) {
        sql = sql.replaceAll('$endDate', formData.endDate)
    }
    sql = sql.replaceAll('$locationId', formData.locationId)
    sql = sql.replaceAll('$invId', formData.invId)

    sql += params.order
    let list = await db.query(sql, {type: QueryTypes.SELECT})

    list.map(row => {
        if(row.isInit === 1) {
            row.transTypeName = '期初'
            row.billDate = ''
            row.buName = ''
            row.inC = ''
            row.inP = ''
            row.inM = ''
            row.outC = ''
            row.outP = ''
            row.outM = ''
            row.lastP =row.lastP? tools.xe.toFixed(row.lastP, 2):''
            row.lastM =row.lastM? tools.xe.toFixed(row.lastM, 2):''
            row.lastC = row.lastC ?row.lastC:''

        } else {
            row.inP =row.inP? tools.xe.toFixed(row.inP, 2):''
            row.inM =row.inM? tools.xe.toFixed(row.inM, 2):''
            row.outP = row.outP?tools.xe.toFixed(row.outP, 2):''
            row.outM = row.outM ? tools.xe.toFixed(row.outM, 2):''
            row.lastP = row.lastP ?tools.xe.toFixed(row.lastP, 2):''
            row.lastM = row.lastM? tools.xe.toFixed(row.lastM, 2):''
            row.inC = row.inC ? row.inC:''
            row.outC = row.outC ? row.outC:''
            row.lastC = row.lastC ? row.lastC:''
        }
    })

    if(excel) {
        return await tools.exportOut(params, list, ctx)
    }
    return ctx.suc('', {
        total: 0,
        list
    })
})


module.exports = router;
